<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/1/1
 * Time: 17:58
 */
namespace common\sources\write\buypepperrecord;

use Yii;
use \common\models\craftsman\BuyPepperRecord;

class BuyPepperRecordAccess implements IBuyPepperRecordAccess
{

	// 添加
	public function addBuyPepperRecord($param) {
		return Yii::$app->db->createCommand()->insert('buy_pepper_record', [
			'buy_id'         => $param['buy_id'],
			'task_order_num' => isset($param['task_order_num']) ? $param['task_order_num'] : '',
			'task_order_id'  => isset($param['task_order_id']) ? $param['task_order_id'] : 0,
			'task_id'        => isset($param['task_id']) ? $param['task_id'] : 0,
			'task_commod_id' => isset($param['task_commod_id']) ? $param['task_commod_id'] : 0,
			'expend'         => $param['expend'],
			'income'         => $param['income'],
			'balance'        => $param['balance'],
            'freeze_change' => isset($param['freeze_change']) ? $param['freeze_change'] : 0,
            'freeze_balance'=> isset($param['freeze_balance']) ? $param['freeze_balance'] : 0,
			'remark'         => $param['remark'],
			'type'           => $param['type'],
			'img_url'        => isset($param['img_url']) ? $param['img_url'] : '',
			'action_type'    => isset($param['action_type']) ? $param['action_type'] : 0,
			'operatorId'     => isset($param['operatorId']) ? $param['operatorId'] : '',
			'auditor_time'   => isset($param['auditor_user']) ? time() : '',
			'auditor_user'   => isset($param['auditor_user']) ? $param['auditor_user'] : '',
			'end_time'       => isset($param['end_user']) ? time() : '',
			'end_user'       => isset($param['end_user']) ? $param['end_user'] : '',
		])->execute();
	}

    public function addPepperRecord($data) {
        $pepperRecord = new BuyPepperRecord();
        $pepperRecord->buy_id = $data['id'];
        $pepperRecord->income = $data['money'];
        $pepperRecord->balance = $data['remail_golds'];
        $pepperRecord->remark = $data['remark'];
        $pepperRecord->operatorId = $data['operatorId'];
        $pepperRecord->order_id = $data['paycode'];
        if (isset($data['img_url'])) {
            $pepperRecord->img_url = $data['img_url'];
        }
        $pepperRecord->action_type = 1;//充值
        $pepperRecord->create_time = time();
        $pepperRecord->update_time = time();
        $pepperRecord->save();
    }

    //TODO:: 已经弃用
    public function addChangePepperRecord($data) {
        $record = new BuyPepperRecord();
        switch ($data['action_type']) {
            case 1://充值
                $record->type = $data['type'];
                $record->buy_id = $data['id'];
                $record->income = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_order_num = $data['task_code'];
	            $record->type = 1;
                $record->create_time = time();
                $record->update_time = time();
                break;
            case 2://扣减
                $record->type = $data['type'];
                $record->buy_id = $data['id'];
                $record->expend = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_order_num = $data['task_code'];
	            $record->type = 2;
                $record->create_time = time();
                $record->update_time = time();
                break;
            case 3://冻结
                $record->buy_id = $data['id'];
                $record->expend = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_order_num = $data['task_code'];
	            $record->type = 4;
                $record->create_time = time();
                $record->update_time = time();
                break;
            case 4://解冻
                $record->buy_id = $data['id'];
                $record->income = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_order_num = $data['task_code'];
	            $record->type = 5;
                $record->create_time = time();
                $record->update_time = time();
                break;
            case 5://扣除解冻
                $record->buy_id = $data['id'];
                $record->expend = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_order_num = $data['task_code'];
	            $record->type = 6;
                $record->create_time = time();
                $record->update_time = time();
                break;
            default:
                break;
        }
        if ($record->save()) {
            return true;
        }
        return false;
    }

    public function addPepperRecord_pop($data) {
        $pepperRecord = new BuyPepperRecord();
        $pepperRecord->buy_id = $data['pop_role_id'];
        $pepperRecord->expend = $data['pepper'];
        $pepperRecord->balance = $data['remail_golds'];
        $pepperRecord->remark = $data['remark'];
        $pepperRecord->operatorId = $data['operatorId'];
        $pepperRecord->task_order_num = $data['task_id'];
        $pepperRecord->img_url = $data['img_url'];
        $pepperRecord->action_type = 2;//扣减
	    $pepperRecord->type = 2;
	    $pepperRecord->create_time = time();
        $pepperRecord->update_time = time();
        $pepperRecord->save();
    }

    public function addPepperRecord_push($data) {
        $pepperRecord = new BuyPepperRecord();
        $pepperRecord->buy_id = $data['push_role_id'];
        $pepperRecord->income = $data['pepper'];
        $pepperRecord->balance = $data['push_pepper'];
        $pepperRecord->remark = $data['remark'];
        $pepperRecord->operatorId = $data['operatorId'];
        $pepperRecord->task_order_num = $data['task_id'];
        $pepperRecord->img_url = $data['img_url'];
        $pepperRecord->action_type = 1;//充值
        $pepperRecord->type = 1;//充值
        $pepperRecord->create_time = time();
        $pepperRecord->update_time = time();
        $pepperRecord->save();
    }

}